import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
const app = getApp()
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        loading: false,
        loaded: false,
        list: [],
        apiField: {
            module: 'powerCard',
            api: 'List',
            data: 'list',
            search: {
                MemberId: null
            }
        },
    },
    onLoad(options) {
        if (options[0]) {
            this.data.apiField.search.MemberId = options[0]
        }
        this.fetchData()
    },
    onShow() {
        if (this.data.needRefresh) {
            this.updateNeedRefresh(false)
            this.fetchData()
        }
    },
    onPullDownRefresh() {
        this.fetchData();
        wx.stopPullDownRefresh();
    },
    async fetchData() {
        this.handleFetchData({}, 'apiField', true)
    },
    handleDelete(e) {
        const { id } = e.currentTarget.dataset;
        var _this = this;
        wx.showModal({
            title: "提示",
            content: "确定要解绑电卡吗？",
            success: async function (showRes) {
                if (showRes.confirm) {
                    _this.doDelete(id);
                }
            },
        });
    },
    async doDelete(id) {
        try {
            const res = await wx.$Api.powerCard.Delete({
                data: {
                    id: id,
                },
                hasLoading: true,
            });

            this.fetchData();
            wx.$Fun.showToast('解绑成功');
        } catch (e) {
            wx.$Fun.showToast(e.Message);
        }
    }
}))